﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddAdvisor : Form
    {
        public AddAdvisor()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            ClientCategory cat = new ClientCategory();
            cat.Code = textBoxCode.Text;
            cat.Description = textBoxDesc.Text;

            try
            {
                ClientCategoryServices.Add(cat);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Client category " + textBoxCode.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new client category\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }
            MessageBox.Show("Client category added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (checkBoxDontClose.Checked == false)
            {
                Close();
            }
            else
            {
                textBoxCode.Text = "";
                textBoxDesc.Text = "";
                textBoxCode.Focus();
            }
        }

        private bool validateInputs()
        {
            if (textBoxCode.Text == "")
            {
                MessageBox.Show("Enter code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (textBoxDesc.Text == "")
            {
                MessageBox.Show("Enter description", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddAdvisor_Load(object sender, EventArgs e)
        {

        }

        private void buttonCancel_Click_1(object sender, EventArgs e)
        {
            Close();
        }
    }
}
